<template>
  <el-dialog
    title="奖惩功能"
    :visible.sync="show"
    :before-close="cancel"
    append-to-body
    :close-on-click-modal="false"
  >
    <el-row class="group-name"
      ><h3>得奖设置</h3>
      <el-button type="primary" plain @click="addPunish('reward')"
        >添加</el-button
      ></el-row
    >
    <div class="rewardList">
      <el-tag
        v-for="item in rewardList"
        :key="item.id"
        closable
        @close="handleClose(item, 'reward')"
        >{{ item.name }}</el-tag
      >
    </div>
    <el-row class="group-name"
      ><h3>惩罚设置</h3>
      <el-button type="primary" plain @click="addPunish('punish')"
        >添加</el-button
      ></el-row
    >
    <div class="rewardList">
      <el-tag
        v-for="item in punishList"
        :key="item.id"
        closable
        @close="handleClose(item, 'punish')"
        >{{ item.name }}</el-tag
      >
    </div>

    <el-dialog
      title="添加"
      :visible.sync="punishFlag"
      :before-close="punishCancel"
      append-to-body
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="ruleForm"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="details">
          <el-input type="textarea" v-model="form.details"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="punishSubmit">确定</el-button>
          <el-button @click="punishCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { createStudentRewardPunish, deleteStudentRewardPunish } from "@/api/collegebase/studentRewardPunish";
import request from "@/utils/request";
import { adminUrl } from "@/utils/baseUrl.js"
export default {
  name: "rewardPunish",
  props: {
    show: {
      type: Boolean,
      default: false,
    },
  },
  created() {
    this.getRewardData();
    this.getPunishData();
  },
  data() {
    return {
      punishFlag: false,
      form: { name: "", details: "", type: "" },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        details: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
      rewardList: [],
      punishList: [],
    };
  },
  methods: {
    cancel() {
      this.$emit("update:show", false);
    },
    //获取得奖列表
    async getRewardData() {
      let res = await request({
        url: adminUrl + "/collegebase/student-reward-punish/byType/list",
        method: "get",
        params: {
          type: "reward",
        },
      });
      this.rewardList = res.data;
    },
    //获取惩罚列表
    async getPunishData() {
      let res = await request({
        url: adminUrl + "/collegebase/student-reward-punish/byType/list",
        method: "get",
        params: {
          type: "punish",
        },
      });
      this.punishList = res.data;
    },
    punishCancel() {
      this.punishFlag = false;
      this.punishrect();
    },
    punishrect() {
      this.form = {
        name: "",
        details: "",
        type: "",
      };
    },
    addPunish(type) {
      this.punishFlag = true;
      this.form["type"] = type;
    },
    punishSubmit() {
      this.$refs["ruleForm"].validate(async (valid) => {
        if (!valid) {
          return;
        }

        let res = await createStudentRewardPunish(this.form);
        if (res.data) {
          this.punishFlag = false;
          this.$modal.msgSuccess("添加成功");

          if (this.form["type"] == "reward") {
            this.getRewardData();
          }
          if (this.form["type"] == "punish") {
            this.getPunishData();
          }
          this.punishrect();
        }
      });
    },
    //删除操作
    handleClose(data, type) {
      console.log(data);
      console.log(type);
      let id = data.id;
      let name = data.name;
      this.$modal
        .confirm('是否确认删除奖罚名称为"' + name + '"的数据项?')
        .then(function () {
          return deleteStudentRewardPunish(id);
        })
        .then(() => {
          if (type == "reward") {
            this.getRewardData();
          }
          if (type == "punish") {
            this.getPunishData();
          }
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    }
  },
};
</script>

<style lang="scss" scoped>
.group-name {
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  h3 {
    margin-right: 50px;
  }
}
.rewardList {
  display: flex;
  align-items: center;
  padding: 0 20px;
  padding-bottom: 10px;
  flex-wrap: wrap;
  border-top: 1px solid #d7d7d7;
  border-bottom: 1px solid #d7d7d7;

  span {
    width: 32%;
    height: 34px;
    line-height: 34px;
    margin-left: 10px;
    text-align: center;
    margin-top: 10px;
  }
}
</style>
